home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 / Programming Windows 95.iso / code / CHAP12 / CTLMACRO.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-01  |  6.0 KB  |  174 lines

  1. /*--------------------------------------------
  2.    CTLMACRO.C -- Control Macro Helper Utility
  3.                  (c) Paul Yao, 1996
  4.   --------------------------------------------*/
  5. #include <windows.h>
  6. #include <windowsx.h>
  7. #include <commctrl.h>
  8. #include <richedit.h>
  9. #include <richole.h>
  10. #include "treeview.h"
  11. #include "resource.h"
  12. #include "notify.h"
  13.  
  14. #define COL_WHITE     (RGB (255, 255, 255))
  15.  
  16. BOOL CALLBACK DlgProc (HWND, UINT, WPARAM, LPARAM) ;
  17.  
  18. HINSTANCE hInst ;
  19.  
  20. //-------------------------------------------------------------------
  21. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, 
  22.                     PSTR lpszCmdLine, int cmdShow)       
  23.      {
  24.      hInst = hInstance ;
  25.  
  26.      // Load common control library
  27.      InitCommonControls () ;
  28.  
  29.      DialogBox (hInstance, MAKEINTRESOURCE (IDD_DIALOG), NULL, DlgProc) ;
  30.  
  31.      return 0 ;
  32.      }
  33.  
  34. //-------------------------------------------------------------------
  35. BOOL CALLBACK
  36. DlgProc (HWND hwnd, UINT mMsg, WPARAM wParam, LPARAM lParam) 
  37.      {
  38.      static BOOL bFirstSelection = TRUE ;
  39.  
  40.      switch (mMsg)
  41.           {
  42.           case WM_INITDIALOG :
  43.                {
  44.                HICON       hicon ;
  45.                HIMAGELIST  himl ;
  46.                HTREEITEM   hItem ;
  47.  
  48.                // Add listview root items
  49.                HWND hwndCtrl = GetDlgItem (hwnd, IDC_MESSAGES) ;
  50.                tv_BuildRootFolder (hwndCtrl) ;
  51.  
  52.                // Select first item
  53.                hItem = TreeView_GetFirstVisible (hwndCtrl) ;
  54.                TreeView_SelectItem (hwndCtrl, hItem) ;
  55.  
  56.                // Create image list & connect to list view
  57.                himl = 
  58.                ImageList_LoadBitmap (hInst,
  59.                                      MAKEINTRESOURCE (IDR_TVITEMS),
  60.                                      16,
  61.                                      0,
  62.                                      COL_WHITE) ;
  63.             
  64.                TreeView_SetImageList (hwndCtrl, himl, TVSIL_NORMAL) ;
  65.             
  66.                // Disable [Copy] button when no macro is present
  67.                hwndCtrl = GetDlgItem (hwnd, IDC_COPY) ;
  68.                EnableWindow (hwndCtrl, FALSE) ;
  69.  
  70.                // Install icon
  71.                hicon = LoadIcon (hInst, MAKEINTRESOURCE (IDI_APP)) ;
  72.                SendMessage (hwnd, WM_SETICON, (WPARAM) (BOOL) TRUE, 
  73.                             (LPARAM) (HICON) hicon) ;
  74.  
  75.                return TRUE ;
  76.                }
  77.  
  78.           case WM_COMMAND :
  79.                switch (LOWORD (wParam))
  80.                     {
  81.                     // Copy macro data to clipboard
  82.                     case IDC_COPY :
  83.                          {
  84.                          HWND hwndEdit = GetDlgItem (hwnd, IDC_MACROS) ;
  85.                          Edit_SetSel (hwndEdit, 0, -1) ;
  86.                          SendMessage (hwndEdit, WM_COPY, 0, 0L) ;
  87.                          break ;
  88.                          }
  89.  
  90.                     // Close dialog
  91.                     case IDOK :
  92.                          EndDialog (hwnd, 0) ;
  93.                          break ;
  94.  
  95.                     // Enable & disable [Copy] button when edit changes
  96.                     case IDC_MACROS :
  97.                          {
  98.                          WORD wNotifyCode = HIWORD (wParam) ;
  99.                          if (wNotifyCode == EN_CHANGE)
  100.                               {
  101.                               HWND hwndEdit = (HWND) lParam ;
  102.                               HWND hwndCopy = GetDlgItem (hwnd, IDC_COPY) ;
  103.                               int  cb = Edit_GetTextLength (hwndEdit) ;
  104.                               EnableWindow (hwndCopy, cb) ;
  105.                               }
  106.                          break ;
  107.                          }
  108.  
  109.                     }
  110.                return TRUE ;
  111.  
  112.           case WM_NOTIFY :
  113.                {
  114.                int idCtrl = (int) wParam ;
  115.                LPNMHDR pnmh = (LPNMHDR) lParam ;
  116.  
  117.                // Expand tree as required
  118.                if (pnmh->code == TVN_ITEMEXPANDING)
  119.                     {
  120.                     LPNM_TREEVIEW pnmtv = (LPNM_TREEVIEW) lParam ;
  121.                     tv_ExpandChild (pnmh->hwndFrom, pnmtv->itemNew.lParam) ;
  122.                     }
  123.  
  124.                // Respond to selection of items
  125.                if (pnmh->code == TVN_SELCHANGED)
  126.                     {
  127.                     LPNM_TREEVIEW pnmtv = (LPNM_TREEVIEW) lParam ;
  128.                     HWND hwndEdit = GetDlgItem (hwnd, IDC_MACROS) ;
  129.  
  130.                     // Ignore first selection request
  131.                     if (bFirstSelection)
  132.                          {
  133.                          bFirstSelection = FALSE ;
  134.                          return TRUE ;
  135.                          }
  136.  
  137.                     // Fetch macro for leaf nodes
  138.                     if (tv_FetchMacro (pnmtv->itemNew.lParam, hwndEdit))
  139.                          {
  140.                          Edit_SetSel (hwndEdit, 0, -1) ;
  141.                          }
  142.                     else
  143.                          {
  144.                          // Expand tree for folder nodes
  145.                          TreeView_Expand (pnmh->hwndFrom, 
  146.                                           pnmtv->itemNew.hItem, 
  147.                                           TVE_EXPAND) ;
  148.                          Edit_SetText (hwndEdit, "") ;
  149.                          }
  150.  
  151.                     return TRUE ;
  152.                     }
  153.  
  154.                // Double-click means copy macro to clipboard
  155.                if (pnmh->code == NM_DBLCLK)
  156.                     {
  157.                     HWND hwndEdit   = GetDlgItem (hwnd, IDC_MACROS) ;
  158.                     HWND hwndButton = GetDlgItem (hwnd, IDC_COPY) ;
  159.                     if (Edit_GetTextLength (hwndEdit) > 0)
  160.                          {
  161.                          FORWARD_WM_COMMAND (hwnd, IDC_COPY, 
  162.                                              hwndButton, BN_CLICKED,
  163.                                              SendMessage) ;
  164.                          }
  165.                     }
  166.  
  167.                return TRUE ;
  168.                }
  169.  
  170.           default :
  171.                return FALSE ;
  172.           }
  173.      }
  174.